package com.demo.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.demo.bean.Asset;
import com.demo.service.AssetReadService;
import com.demo.service.JsonManageService;
import com.demo.service.RackReadService;
import com.demo.service.impl.AssetReadServiceImpl;
import com.demo.service.impl.JsonManageServiceImpl;
import com.demo.service.impl.RackReadServiceImpl;
import com.demo.util.ActiveMQPusher;
import com.demo.util.JsonExcels;
import net.sf.json.JSON;
import net.sf.json.JSONArray;

/**
 * @author 熊雨会
 * @date 2017年7月7日 下午6:24:39
 * @class com.demo.controller.HelloController
 * @用处 ：
 */

@Controller
public class FileUploadController {
	/**
	 * 场景编号
	 */
	static String sceneID = null;
	/**
	 * 架式设备之间间隔随机数
	 */
	static int JG = 4;
	/**
	 * 机柜高度
	 */
	static int GD = 42;
	/**
	 * 推送ip
	 */
	static String IP = "127.0.0.1";
	/**
	 * 机柜集合
	 */
	static List<String> rackList;
	/**
	 * 资产集合
	 */
	static List<Asset> assetList;
	/**
	 * 房间集合
	 */
	static List<String> fjlist;

	/**
	 * ITvop上传参数，以下参数赋值
	 * 
	 * @param text     场景ID
	 * @param ip       推送ip
	 * @param fangjian 房间编号
	 * @param G        机柜高度
	 * @param J        架式设备之间间隔随机数
	 * @return
	 */
	@RequestMapping("/upload")
	public String handleFileUpload(String text, String ip, String fangjian, String G, String J) {
		sceneID = null;
		if (null != text && !"".equals(text)) {
			sceneID = text;

			if (null != ip && !"".equals(ip)) {
				IP = ip;
			}
			if (null != G && !"".equals(G)) {
				GD = Integer.parseInt(G);
			}
			if (null != J && !"".equals(J)) {
				JG = Integer.parseInt(J);
			}
			System.out
					.println("--------------------------------------------------------------------------------------");
			System.out.println("房间编号:");
			if (fangjian != null) {
				fjlist = new ArrayList<String>();
				String[] sourceStrArray = fangjian.split(",");
				for (int i = 0; i < sourceStrArray.length; i++) {
					if (!"".equals(sourceStrArray[i])) {
						fjlist.add(sourceStrArray[i]);
						System.out.print(sourceStrArray[i]);
					}
				}
			}
			System.out.println("数据中心ID:" + sceneID + "  " + "推送ip:" + IP + "  " + "机柜高度:" + GD + "  " + "设备间隔:" + JG);
			return "zichan_two.html";
		}
		return "zichan_one.html";
	}

	/**
	 * Tarsier-上传参数，以下参数赋值
	 * 
	 * @param fangjian 房间编号
	 * @param G        机柜高度
	 * @param J        架式设备之间间隔随机数
	 * @return
	 */
	@RequestMapping("/uploads")
	public String handleFileUploads(String fangjian, String G, String J) {
		if (null != G && !"".equals(G)) {
			GD = Integer.parseInt(G);
		}
		if (null != J && !"".equals(J)) {
			JG = Integer.parseInt(J);
		}
		System.out.println("--------------------------------------------------------------------------------------");
		System.out.println("房间编号:");
		if (fangjian != null) {
			fjlist = new ArrayList<String>();
			String[] sourceStrArray = fangjian.split(",");
			for (int i = 0; i < sourceStrArray.length; i++) {
				if (!"".equals(sourceStrArray[i])) {
					fjlist.add(sourceStrArray[i]);
					System.out.print(sourceStrArray[i]);
				}
			}
		}
		System.out.println("机柜高度:" + GD + "  " + "设备间隔:" + JG);
		return "zichan_twos.html";
	}

	/**
	 * itvop上传布局文件
	 * 
	 * @param file
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping(value = "/uploadJson", method = RequestMethod.POST)
	public String uploadJson(HttpServletRequest request, @RequestParam("file") MultipartFile file)
			throws IllegalStateException, IOException {
		RackReadService rackReadService = new RackReadServiceImpl();
		rackList = new ArrayList<String>();

		if (!file.isEmpty()) {
			// 上传文件路径
			String path = request.getServletContext().getRealPath("/upload");
			// 上传文件名
			String filename = file.getOriginalFilename();
			File filepath = new File(path, filename);
			// 判断路径是否存在，如果不存在就创建一个
			if (!filepath.getParentFile().exists()) {
				filepath.getParentFile().mkdirs();
			}
			// 将上传文件保存到一个目标文件当中
			file.transferTo(new File(path + File.separator + filename));
			String filePath = path + File.separator + filename;
			System.out.println("文件上传成功");
			System.out.println("文件路径：" + path + File.separator + filename);
			if (fjlist.size() != 0) {
				rackList = rackReadService.readJson(filePath, fjlist);
			} else {
				rackList = rackReadService.readJson(filePath);
			}
			System.out.println("机柜编号:" + rackList);
			return ("zichan_three.html");
		}
		return ("zichan_two.html");
	}

	/**
	 * tarsier上传布局文件
	 * 
	 * @param file
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping(value = "/uploadJsons", method = RequestMethod.POST)
	public String uploadJsons(HttpServletRequest request, @RequestParam("file") MultipartFile file)
			throws IllegalStateException, IOException {
		RackReadService rackReadService = new RackReadServiceImpl();
		rackList = new ArrayList<String>();

		if (!file.isEmpty()) {
			// 上传文件路径
			String path = request.getServletContext().getRealPath("/upload");
			// 上传文件名
			String filename = file.getOriginalFilename();
			File filepath = new File(path, filename);
			// 判断路径是否存在，如果不存在就创建一个
			if (!filepath.getParentFile().exists()) {
				filepath.getParentFile().mkdirs();
			}
			// 将上传文件保存到一个目标文件当中
			file.transferTo(new File(path + File.separator + filename));
			String filePath = path + File.separator + filename;
			System.out.println("文件上传成功");
			System.out.println("文件路径：" + path + File.separator + filename);
			if (fjlist.size() != 0) {
				rackList = rackReadService.readJson(filePath, fjlist);
			} else {
				rackList = rackReadService.readJson(filePath);
			}
			System.out.println("机柜编号:" + rackList);
			return ("zichan_threes.html");
		}
		return ("zichan_twos.html");
	}

	/**
	 * itvop上传设备型号清单
	 * 
	 * @param file        设备型号清单.exl-路径
	 * @param zichanshuju 判断复选框是否选中
	 * @param tarsier     判断复选框是否选中
	 * @return
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	@RequestMapping(value = "/uploadExcel", method = RequestMethod.POST)
	public String uploadExcel(HttpServletRequest request, @RequestParam("file") MultipartFile file, String zichanshuju,
			String tarsier) throws IllegalStateException, IOException {
		assetList = new ArrayList<Asset>();
		AssetReadService assetReadService = new AssetReadServiceImpl();
		if (!file.isEmpty()) {
			// 上传文件路径
			String path = request.getServletContext().getRealPath("/upload");
			// 上传文件名
			String filename = file.getOriginalFilename();
			File filepath = new File(path, filename);
			// 判断路径是否存在，如果不存在就创建一个
			if (!filepath.getParentFile().exists()) {
				filepath.getParentFile().mkdirs();
			}
			// 将上传文件保存到一个目标文件当中
			file.transferTo(new File(path + File.separator + filename));
			String filePath = path + File.separator + filename;
			System.out.println("文件上传成功");
			System.out.println("文件路径：" + path + File.separator + filename);

			assetList = assetReadService.readAsset(filePath);
			System.out.println("资产清单:" + JSONArray.fromObject(assetList));
			JsonManageService service = new JsonManageServiceImpl();
			JSON zcjson = service.JsonManage(assetList, rackList, sceneID, JG, GD);

			ActiveMQPusher assetmq = new ActiveMQPusher("admin", "admin", "tcp://" + IP + ":61616", "assetQueue");
			System.out.println("资产数据:" + zcjson.toString());
			assetmq.push2ActiveMQ(zcjson.toString());
			System.gc();
			return "zichan_succeed.html";
		}
		return "zichan_three.html";
	}

	/**
	 * Tarsier-上传设备型号清单
	 * 
	 * @param file        设备型号清单.exl-路径
	 * @param zichanshuju 判断复选框是否选中
	 * @param tarsier     判断复选框是否选中
	 * @return
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	@RequestMapping(value = "/uploadExcels", method = RequestMethod.POST)
	public String uploadExcels(HttpServletRequest request, @RequestParam("file") MultipartFile file)
			throws IllegalStateException, IOException {
		assetList = new ArrayList<Asset>();
		AssetReadService assetReadService = new AssetReadServiceImpl();
		if (!file.isEmpty()) {
			// 上传文件路径
			String path = request.getServletContext().getRealPath("/upload");
			// 上传文件名
			String filename = file.getOriginalFilename();
			File filepath = new File(path, filename);
			// 判断路径是否存在，如果不存在就创建一个
			if (!filepath.getParentFile().exists()) {
				filepath.getParentFile().mkdirs();
			}
			// 将上传文件保存到一个目标文件当中
			file.transferTo(new File(path + File.separator + filename));
			String filePath = path + File.separator + filename;
			System.out.println("文件上传成功");
			System.out.println("文件路径：" + path + File.separator + filename);

			assetList = assetReadService.readAsset(filePath);
			System.out.println("资产清单:" + JSONArray.fromObject(assetList));
			JsonManageService service = new JsonManageServiceImpl();
			JSON tarsierzcjson = service.JsonDataManage(assetList, rackList, JG, GD);
			System.out.print("状态:");
			JsonExcels.print(tarsierzcjson.toString(), "Tarsier资产数据");
			System.gc();
			return "tarsier_zichan_succeed.html";
		}
		return "zichan_threes.html";
	}
}
